package com.qvtu.web.controller;


import com.qvtu.web.config.AjaxResult;
import com.qvtu.web.domain.Books;
import com.qvtu.web.service.IBookCategroyService;
import com.qvtu.web.service.IBookStatusService;
import com.qvtu.web.service.IBooksService;
import com.qvtu.web.service.IPublisherService;
import org.apache.catalina.WebResourceRoot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hyy
 * @since 2024-06-14
 */
@Controller
@RequestMapping("/books")
public class BooksController {
    @Autowired
    private IBooksService booksService;
    @Autowired
    private IPublisherService publisherService;
    @Autowired
    private IBookStatusService bookStatusService;
    @Autowired
    private IBookCategroyService bookCategroyService;



    @RequestMapping("/books/save")
    public String save()
    {
        return  "MainInterface/books_save";
    }

    @RequestMapping("/edit{id}")
    public String booksEdit(Model model,@PathVariable("id") Integer num){
        Books booksList =booksService.getById(num);
        model.addAttribute("booksList",booksList);
        List publisherList =publisherService.list();
        model.addAttribute("publisherList",publisherList);

        List statusList =bookStatusService.list();
        model.addAttribute("statusList",statusList);

        List categroyList =bookCategroyService.list();
        model.addAttribute("categroyList",categroyList);

        return "MainInterface/books_save";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Long id){
        booksService.removeById(id);
        return AjaxResult.success();
    }

    @RequestMapping("/saveOrUpdata")
    @ResponseBody
    public AjaxResult update(Books books){
        if(books.getId() == null){
            books.setCreateTime(LocalDateTime.now());
            books.setUpdataTime(LocalDateTime.now());
        }else{
            books.setUpdataTime(LocalDateTime.now());
        }
        booksService.saveOrUpdate(books);
        return AjaxResult.success();
    }

}

